﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using F1010DB.CustomEntity.Account;
namespace F1010DB.Account
{
    public abstract class AbstractAccountLogin: AbstractGetBridgeConnect
    {
        
        private AccountLogin accountLogin;

        protected abstract AccountLogin setAccountLoginModel();

        protected static class ScriptForAccountLogin
        {
            public const string CHECK_EXIST_ACCOUNT = "SELECT A.UNIT_CODE, A.PASSWORD, B.TEN_DON_VI, B.EMAIL, B.EMAIL_2, C.MA_NGANH, C.MAX_AGE, C.MIN_AGE "
                                                    + " FROM HDVNACC.UNIT_ACCOUNT A, HDVNQTRI.DON_VI_MASTER B, HDVNSYS.DANH_MUC_NGANH_HUONG_DAO C WHERE A.UNIT_CODE = B.MA_DON_VI AND B.NGANH = C.MA_NGANH AND A.UNIT_CODE = @unitcode AND A.LOCK = '0'";
            public const string GET_ACCOUNT_INFO = "SELECT A.MA_DON_VI, A.TEN_DON_VI, A.TINH_THANH_PHO, A.CAP_DON_VI, A.EMAIL, A.EMAIL_2, A.SO_DIEN_THOAI_DD, A.NGANH, B.TEN_NGANH, B.MAX_AGE, B.MIN_AGE, A.MA_DON_VI_CHA "
                                                + " FROM HDVNQTRI.DON_VI_MASTER AS A , "
                                                + "  HDVNSYS.DANH_MUC_NGANH_HUONG_DAO AS B "
                                                + " WHERE A.CAP_DON_VI = 3 AND A.NGANH = B.MA_NGANH AND A.MA_DON_VI = @madonvi";                                
        }

        private void setAccountLogin()
        {
            this.accountLogin = setAccountLoginModel();
        }
        protected List<string> checkLoginInput()
        {
            setAccountLogin();
            List<string> tmpMsg = new List<string>();

            if ( accountLogin.AccountId == string.Empty || accountLogin.Password == string.Empty)
            {
                tmpMsg.Add(" Mã đơn vị / mật khẩu chưa được nhập bạn không được bỏ trống");
            }
            else
            {
               
                Dictionary<string, object> obj = new Dictionary<string, object>();

                obj.Add("@unitcode", accountLogin.AccountId);

                DataTable dtResult = dbContext.readATable(ScriptForAccountLogin.CHECK_EXIST_ACCOUNT, CommandType.Text, obj);


                if (dtResult == null || dtResult.Rows.Count == 0)
                {
                    tmpMsg.Add("Đơn vị không tồn tại hoặc đã khóa rồi");
                }
                else
                {
                    string currentPassword = EncryptDataAndDecryptData.Encrypt(accountLogin.Password, true, accountLogin.AccountId);
                    string dbPassword = dtResult.Rows[0]["PASSWORD"].ToString();

                    if (currentPassword.Equals(dbPassword) == false)
                    {
                        tmpMsg.Add("Mật khẩu nhập sai");
                    }
                }
            }

            if (tmpMsg.Count == 0)
            {
                return null;
            }

            return tmpMsg;
        }

        protected void setAccountInfoForSeeing(string maDonVi)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            dic.Add("@madonvi", maDonVi);

            DataTable dtAccountInfo = dbContext.readATable(ScriptForAccountLogin.GET_ACCOUNT_INFO, CommandType.Text, dic);
           
            DataRow dr = dtAccountInfo.Rows[0];

            AccountInfo.accountId = dr["MA_DON_VI"].ToString();
            AccountInfo.tenDonVi = dr["TEN_DON_VI"].ToString();
            AccountInfo.email = dr["EMAIL"].ToString();
            AccountInfo.email2 = dr["EMAIL_2"].ToString();
            AccountInfo.level = dr["TEN_NGANH"].ToString();
            AccountInfo.levelCode = dr["NGANH"].ToString();
            AccountInfo.maxAge = Int16.Parse(dr["MAX_AGE"].ToString());
            AccountInfo.minAge = Int16.Parse(dr["MIN_AGE"].ToString());
            AccountInfo.maLienDoan = dr["MA_DON_VI_CHA"].ToString();
            AccountInfo.capDonVi = dr["CAP_DON_VI"].ToString();
            AccountInfo.location = Int16.Parse(dr["TINH_THANH_PHO"].ToString());
        }
    }

    
}
